using System;
using System.Runtime.CompilerServices;
using jQueryApi;
using Debug = Nsb.Debug;

namespace Yoshi.Fb
{
    public class FbImgData : FbIdentity
    {
        private const string MultipartBoundry = "zxyxxx894xxxzxycannonplayopolis";
        private object rawGif;

        [AlternateSignature]
        public extern void PostToFb(object gif);

        public void PostToFb(object gif, FnDelegate fn)
        {
            rawGif = gif;
            jQueryAjaxOptions request = new jQueryAjaxOptions();

            request.Url = "https://graph.facebook.com/" + user.id + "/photos";
            request.Type = "POST";
            //request.DataType = "jsonp";
            request.ContentType = "multipart/form-data; boundary=" + MultipartBoundry;
            request.ProcessData = false;

            string[] names = (string[])new Array();
            string[] values = (string[])new Array();

            names[0] = "access_token";
            //names[1] = "message";
            names[1] = "userfile";

            values[0] = auth.access_token;
            //values[1] = "image";
            values[1] = "";

            request.Data = MultipartData(names, values);

            //Debug.Log("gif length={0}", gif.Length);
            Debug.Log("request.Data=\r\n{0}", request.Data);


            request.Success =
                delegate(object data, string textStatus, jQueryXmlHttpRequest request1)
                {
                    YoshiGif result = (YoshiGif)data;
                    if (Script.IsNullOrUndefined(result) || result.result == false)
                    {
                        Debug.Error(result.msg);
                        return;
                    }
                    Debug.Log("Data photo post success");
                    if (Script.IsValue(fn)) fn(data);
                };

            request.Error =
                delegate
                {
                    Debug.Error("Error posting data image");
                };

            jQuery.Ajax(request);
        }

        public object MultipartData(string[] names, string[] data)
        {
            //Start multipart formatting
            const string strBoundary = "--" + MultipartBoundry;
            string strMultipartBody = "";
            const string strCrlf = "\r\n";
            object payload;

            for (int i = 0; i < names.Length; i++)
            {
                string contentHead;
                if (i == (names.Length - 1))
                {
                    contentHead =
                        //"Content-Disposition: form-data; name=\"file\"; filename=\"abc.gif\"" +
                        "Content-Disposition: form-data; filename=\"abc.gif\"" +
                        //"Content-Disposition: form-data; name=\"abc.gif\"" +
                        strCrlf +
                        "Content-Type: image/gif" +
                        //"Content-Type: application/octet-stream" +
                        strCrlf +
                        //"Content-Transfer-Encoding: binary";
                        "Content-Transfer-Encoding: base64";
                    //contentHead = "Content-Type: application/octet-stream" + strCrlf + "Content-Transfer-Encoding: base64";
                    payload = rawGif;
                }
                else
                {
                    contentHead = "Content-Disposition: form-data; name=\"" + names[i] + "\"";
                    payload = data[i];
                }

                strMultipartBody +=
                    strBoundary +
                    strCrlf +
                    contentHead +
                    strCrlf +
                    strCrlf +
                    payload +
                    strCrlf;
            }
            return strMultipartBody + strBoundary + "--" + strCrlf;
        }
    }
}
